package userInterface.Panels;

import dataAccess.*;
import insim.*;
import settings.*;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;

/**
 * UI JPanel for running automated tests.
 * @author BDS
 */
public class PanelMain extends PanelBase
{
	private static final long serialVersionUID = 1L;
	
	private ActionListener mainActionListener;
	private DefaultTreeCellRenderer mainTreeCellRenderer;
	
	private SpringLayout slMainLayout;
	
	private JScrollPane scpStatus;
	private JTree statusTree;
	private JButton btnRun;

	/**
	 * Class constructor.
	 */
	public PanelMain()
	{
		mainActionListener = new MainActionListener();
		mainTreeCellRenderer = new MainTreeCellRenderer();
		
		slMainLayout = new SpringLayout();
		setLayout(slMainLayout);
		
		scpStatus = new JScrollPane();
		slMainLayout.putConstraint(SpringLayout.WEST, scpStatus, 10, SpringLayout.WEST, this);
		slMainLayout.putConstraint(SpringLayout.NORTH, scpStatus, 10, SpringLayout.NORTH, this);
		slMainLayout.putConstraint(SpringLayout.EAST, scpStatus, -10, SpringLayout.EAST, this);
		slMainLayout.putConstraint(SpringLayout.SOUTH, scpStatus, -50, SpringLayout.SOUTH, this);
		add(scpStatus);
		
		btnRun = new JButton("Run");
		btnRun.setMnemonic(KeyEvent.VK_R);
		btnRun.addActionListener(mainActionListener);
		slMainLayout.putConstraint(SpringLayout.HORIZONTAL_CENTER, btnRun, 0, SpringLayout.HORIZONTAL_CENTER, this);
		slMainLayout.putConstraint(SpringLayout.SOUTH, btnRun, -10, SpringLayout.SOUTH, this);
		add(btnRun);
	}
		
	/**
	 * Reloads all data.
	 */
	@Override
	public void load() 
	{ 
		Settings settings = LocalDataAccess.loadSettings();
		statusTree = new JTree();
		statusTree.setRootVisible(false);
		statusTree.setCellRenderer(mainTreeCellRenderer);
		
		DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Status");
		rootNode.add(new DefaultMutableTreeNode("Initializing"));
		
		for (Environment environment : settings.getEnvironments())
		{
			DefaultMutableTreeNode environmentNode = new DefaultMutableTreeNode(environment.getName());
			
			for (Workflow workflow : environment.getWorkflows())
			{
				DefaultMutableTreeNode workflowNode = new DefaultMutableTreeNode(workflow.getName());
				
				for (Command command : workflow.getCommands())
				{
					DefaultMutableTreeNode commandNode = new DefaultMutableTreeNode(command.toString());
					
					workflowNode.add(commandNode);
				}
				
				environmentNode.add(workflowNode);
			}
			
			rootNode.add(environmentNode);
		}

		rootNode.add(new DefaultMutableTreeNode("Saving session data"));
		rootNode.add(new DefaultMutableTreeNode("Sending email reports"));
		rootNode.add(new DefaultMutableTreeNode("Complete"));
		
		statusTree.setModel(new DefaultTreeModel(rootNode));
		scpStatus.setViewportView(statusTree);
	}
	
	/**
	 * ActionListener for the Run JButton.
	 * @author BDS
	 */
	private class MainActionListener implements ActionListener 
	{
		/**
		 * Runs automated tests.
		 * @see insim.Main#run()
		 */
		@Override
		public void actionPerformed(ActionEvent actionEvent)
		{
			getParent().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
			Main.run();
			getParent().setCursor(Cursor.getDefaultCursor());
		}
	}
	
	/**
	 * DefaultTreeCellRenderer for the status JTree.
	 * @author BDS
	 */
	public class MainTreeCellRenderer extends DefaultTreeCellRenderer 
	{
		private static final long serialVersionUID = 1L;
		
		/**
		 * TODO Strikethrough all actions which are disabled via settings with setFont().
		 */
	    @Override
	    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) 
	    {
	        super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
	 
	        return this;
	    }
	}
}
